public class LodhaFormController {

    public LodhaFormController(ApexPages.StandardController controller) {
    
     PicklistValues = new List<String>();
             PicklistValues = getPicklistValues('Project__c','Check_Exhaust_fan_properly_fitted_work__c');
                         project=new Project__c();
                        Picklist1=new List<String>();
                        Picklist2=new List<String>();
                        Picklist3=new List<String>();
                        Picklist4=new List<String>();
                        Picklist5=new List<String>();
                        Picklist6=new List<String>();
                        
                        
                        
                        snaglist=new SnagList__c();
                        objectandFielddescribe();
                        
                        
                        
                        
                        
                         String flatId=ApexPages.currentPage().getParameters().get('Flat_Id');
                         
                         Flat__c flat=[select Name, Wing__r.Name,Wing__r.Building__r.Name from flat__c where id=:flatId];
                      
                               flatnew=flat.Name;
                               wingnew=flat.Wing__r.Name;
                               buildingnew=flat.Wing__r.Building__r.Name;
                               wingId=flat.Wing__c;
                               //buildingId=Wing__r.Building__r.Id;
                               
                               Building__c buildinglist=[select Name,Project_Name__r.Name from Building__c where Name=:buildingnew];
                               projectnamenew=buildinglist.Project_Name__r.Name;
                               
                              
    }
       





            public List<Selectoption> options{get;set;}
            
            public String projectname{get;set;}
            public String projectnamenew{get;set;}
            
            public String building{get;set;}
            public String buildingnew{get;set;}            
            public String buildingId{get;set;}
            
            public String wing{get;set;}
            public String wingnew{get;set;}
            public String wingId{get;set;}
            
            
            public String Values{get;set;}
            public String flat{get;set;}
            public String flatnew{get;set;}
            public String projectId{get;set;}
            
            
            public  Schema.sObjectType sobject_type{get;set;}
            public Transient Schema.DescribeSObjectResult sobject_describe{get;set;}
            public Map<String, Schema.SObjectField> field_map{get;set;}
            public Set<String> Field_Name {get;set;}
            Public List<fieldvalues> alldatatype{get;set;}
            
            public List<fieldvalues> fvalues{get;set;}


             public List<String> PicklistValues{get;set;}
             public List<String> Picklist1{get;set;}
             public List<String> Picklist2{get;set;}
             public List<String> Picklist3{get;set;}
             public List<String> Picklist4{get;set;}
             public List<String> Picklist5{get;set;}
             public List<String> Picklist6{get;set;}
             public Project__c project{get;set;}
             public Project__c projectlist{get;set;}
             public SnagList__c snaglist{get;set;}             
             public List<snag> SnagWrapList {get;set;}
 
             /*public LodhaFormController(){


             PicklistValues = new List<String>();
             PicklistValues = getPicklistValues('Project__c','Check_Exhaust_fan_properly_fitted_work__c');
                         project=new Project__c();
                        Picklist1=new List<String>();
                        Picklist2=new List<String>();
                        Picklist3=new List<String>();
                        Picklist4=new List<String>();
                        Picklist5=new List<String>();
                        Picklist6=new List<String>();
                        
                        
                        snaglist=new SnagList__c();
                      
             }*/
             
             public class Snag{
             
             public String snagdesc{get;set;}
             public String snaglocation{get;set;}
             public String snagchecklist{get;set;}
             
             
             }                                   
             
             public class fieldvalues{
                
                       public Schema.DisplayType FType {get;set;}
                       public String Fname{get;set;}
                
                
                }
                
                
                
                public void objectandFielddescribe()
                    {
                          sobject_type = project__c.getSObjectType();
                          sobject_describe = sobject_type.getDescribe();
                          field_map = sobject_describe.fields.getMap();
                          Field_Name=field_map.keyset();
                     
                            
                           alldatatype=new List<fieldvalues>();
                           
                           alldatatype.clear();
                           for(Schema.SObjectField fil:field_map.values())
                            {
                                
                                fieldvalues fvalues=new fieldvalues();
                                
                                 schema.describefieldresult results=fil.getDescribe();
                                 //datatype=results.getType();
                                 fvalues.Ftype=results.getType();
                                 fvalues.Fname=results.getName();
                                 if(results.getType()==Schema.DisplayType.MULTIPICKLIST){
                                     alldatatype.add(fvalues);
                                 }
                            }
                    }
 
     public List<SelectOption> getItems() {
        
             options = new List<SelectOption>();
                
                
               // for(Schema.PicklistEntry p :schema.getGlobalDescribe().get('Project__c').getDescribe().fields.getMap().get('Door_frame_should_be_in_plumb__c').getDescribe().getPicklistValues())
                    for(String p:PicklistValues)
                    {
                          options.add(new SelectOption(p,p));
                    }                  
                        return options;
            }
     
    
    
    public List<String> getPickListvalues(String objectName,String fieldName){
        List<string> picklistValues = new List<String>();
        for(Schema.PicklistEntry p :schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()){
                          picklistValues.add(p.getlabel());
        }
        return picklistValues;
    } 
    
    
    
    
    

           public PageReference savemethod(){ 
           
                        String values1='';
                        String values2='';
                        String values3='';
                        String values4='';
                        String values5='';
                        String values6='';
                        
                        
                        
                        
                        for(string s:picklist1){
                        
                         values1=values1+';'+s;
                            
                        }
                        
                         for(string s:picklist2){
                        
                         values2=values2+';'+s;
                            
                        }
                        
                         for(string s:picklist3){
                        
                         values3=values3+';'+s;
                            
                        }
                         for(string s:picklist4){
                        
                         values4=values4+';'+s;
                            
                        }
                         for(string s:picklist5){
                        
                         values5=values5+';'+s;
                            
                        }
                         for(string s:picklist6){
                        
                         values6=values6+';'+s;
                            
                        }
                        
                             /*project.Name=[select name from project_name__c where id=:projectname].name;
                             project.Building__c=building;
                             project.Wing__c=building;
                             project.Flat__c=flat;*/
                             
                             
                                 project.Name=projectnamenew;
                                 project.Building__c=buildingnew;
                                 project.Wing__c=wingnew;
                                 project.Flat__c=flatnew;
                     
                     
                               project.Door_frame_should_be_in_plumb__c=values1;
                               project.Sliding_doors_proper_gasketing__c=values2;
                               project.Proper_gasketing_if_applicable__c=values3;
                               project.Window_lock_working_properly__c=values4;
                               project.Check_the_main_supply_for_sufficient_vo__c=values5;
                               project.Check_Exhaust_fan_properly_fitted_work__c=values6;
                         
                         
            /*List<String> exhaustFanValues = project.Check_Exhaust_fan_properly_fitted_work__c.split(';');
            
            List<String> exhaustFanValues = project.Door_frame_should_be_in_plumb__c.split(';');
            
            Values='';
            
            for(String s:PicklistValues){
                Boolean val=false;
                for(String s1:exhaustFanValues){
                  if((s1==s)){ 
                      val= True; 
                  }
                }
                if(!val){
                   values = values+';'+s;
                }
            }*/
     
                 
                        
           return test(alldatatype);
           
           }
    
    
    
    public List<selectoption> getQueryforproject(){
    
        List<selectoption> projectoptions= new List<selectoption>();
    
    
        
        projectoptions.add(new SelectOption('--Select Project--','--Select Project--'));
        List<Project_Name__c> projectname=[select Name from Project_Name__c];
    
            for(Project_Name__c s:projectname){
            
                projectoptions.add(new SelectOption(s.id,s.name));

            
            }
            return projectoptions;
    }
    
        public List<selectoption> getQueryforbuilding(){
    
        List<selectoption> buildingoptions= new List<selectoption>();
        
        buildingoptions.add(new SelectOption('--Select Building--','--Select Building--'));

        List<Building__c> buildingname=[select Name from Building__c where Project_Name__c=:projectname];
    
            for(Building__c s:buildingname){
            
                buildingoptions.add(new SelectOption(s.id,s.name));

            
            }
            return buildingoptions;
    
    }
     
     public List<selectoption> getQueryforwing(){
    
        List<selectoption> wingoptions = new List<selectoption>();
             wingoptions.add(new SelectOption('--Select Wing--','--Select Wing--'));

        List<Wing__c> wingname=[select Name from Wing__c where Building__c=:building];
    
            for(wing__c s:wingname){
            
                wingoptions.add(new SelectOption(s.id,s.name));

            
            }
    return wingoptions;
               
    }
    
    
       public List<selectoption> getQueryforflat(){
    
        List<selectoption> flatoptions= new List<selectoption>();
        flatoptions.add(new SelectOption('--Select Flat--','--Select Flat--'));
        List<Flat__c> flatname=[select Name from flat__c where wing__c=:wing and Status__c='Ready for Snagging'];
    
            for(Flat__c s:flatname){
            
                flatoptions.add(new SelectOption(s.id,s.name));

            
            }
    
            return flatoptions;   
    }
    
    
    /*public void querymethod(){
    
    projectlist=[select Check_Exhaust_fan_properly_fitted_work__c,Check_the_main_supply_for_sufficient_vo__c,Door_frame_should_be_in_plumb__c,
    Proper_gasketing_if_applicable__c,Sliding_doors_proper_gasketing__c,Window_lock_working_properly__c from Project__c where id=:projectId];
    
    
        if(projectlist!=null){
            Values='';
            String exhaustFanValues = projectlist.Check_Exhaust_fan_properly_fitted_work__c;
            for(String s:PicklistValues){
               if(!exhaustFanValues.contains(s)){
                   
                  Values=Values+s; 
                   
               }
            }
        }
    }       
           
     public PageReference savesnaglist(){
     
         //Project__c.Name=id;
         
         insert snaglist;
         return null;
     
     } */     
           
           
           
           
           
           
    public PageReference test(List<fieldvalues> fvalue) {
    
    SnagWrapList =new List<Snag>();
    
    
    //List<String> exhaustFanValues = project.Check_Exhaust_fan_properly_fitted_work__c.split(';');
    
    List<fieldvalues> fvalues=fvalue;
    
   
    
    
            for(fieldvalues fname: fvalue){
            
            String Fieldapiname=fname.Fname;
            object o = project.get(Fieldapiname);
            Fieldapiname = (string) o;
            
            List<String> exhaustFanValues = Fieldapiname.split(';');   
                    
            Values='';
            
            for(String s:PicklistValues){
                Boolean val=false;
                for(String s1:exhaustFanValues){
                  if((s1==s)){ 
                      val= True; 
                  }
                }
                if(!val){
                   values = values+';'+s;
                }
            }
            
            
            for(String v:values.split(';') ){
                Snag s = new snag();
                
                if(v!=''&&v!=null){
                
                    s.snaglocation=v;  
                     
                    if(fname.Fname=='Door_frame_should_be_in_plumb__c')
                    {
                     s.snagchecklist ='A1';
                    }  
                    else if(fname.Fname=='Sliding_doors_proper_gasketing__c')
                    {
                     s.snagchecklist ='A2';
                    }
                    else if(fname.Fname=='Proper_gasketing_if_applicable__c')
                    {
                     s.snagchecklist ='B1';
                    } 
                    else if(fname.Fname=='Window_lock_working_properly__c')
                    {
                     s.snagchecklist ='B2';
                    }
                    else if(fname.Fname=='Check_the_main_supply_for_sufficient_vo__c')
                    {
                     s.snagchecklist ='C1';
                    }
                    else if(fname.Fname=='Check_Exhaust_fan_properly_fitted_work__c')
                    {
                     s.snagchecklist ='C2';
                    } 
                               
                   
                    SnagWrapList.add(s);
                }
               
                           
            }
            }
            system.debug('$$$$$$$'+fvalues);
           // PageReference pageRef = new PageReference('/apex/snaglist');
            PageReference pageRef = new PageReference('http://www.google.com');
            pageRef.setredirect(false);
  
            return page.snaglist;
    }
    
    public pagereference saverecord(){
    
    
        insert project;
    
        
        
        List<SnagList__c> slist=new List<SnagList__c>();
        for(snag sn:SnagWrapList)
        {
        
        slist.add(new SnagList__c(Checklist_Tag__c=sn.snagchecklist,Location__c=sn.snaglocation,Project__c=project.id,Snag_Description__c=sn.snagdesc));
        
        
        
        }
        insert slist;
         return null;
    
    }
 

}